package com.doform.android.xianggucheng.activity.downorder;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.AddressBean;
import com.doform.android.xianggucheng.bean.GuidBean;
import com.doform.android.xianggucheng.bean.ShopCarBean;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.widget.DividerItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/10.
 */

public class SubmitOrderActivity extends BaseTitleActivity {

    @BindView(R.id.ll)
    LinearLayout mLl;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_sum_price)
    TextView mSumPrice;

    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.tv_address)
    TextView tv_address;

    private List<ShopCarBean.ListBean> mDatas = new ArrayList<>();
    private CommonAdapter<ShopCarBean.ListBean> mCommonAdapter;
    private String mClientBeanId;
    private AddressBean mAddrBean;
    private String mStrSumPrice;

    @Override
    protected void showView() {
        titleBar.setTextTitle("提交订单");
        if (mAddrBean != null) {
            tv_name.setText(mAddrBean.getLnkname());
            tv_name.setTextColor(getResources().getColor(R.color.black));
            tv_address.setText(mAddrBean.getAddr());
            tv_address.setTextColor(getResources().getColor(R.color.black));
        }
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_submit_order;
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    protected void onStart() {
        super.onStart();
        getDateForService();
    }

    private void getDateForService() {
        HttpManager.instance()
                .getShopCarData("1")
                .subscribe(new ErrorHandleSubscriber<ShopCarBean>() {
                    @Override
                    public void onNext(@NonNull ShopCarBean shopCarBean) {
                        parseData(shopCarBean);
                    }
                });
    }

    private void parseData(ShopCarBean bean) {
        mStrSumPrice = bean.getTotal() + "";
        if (TextUtils.isEmpty(mStrSumPrice)) {
            mSumPrice.setText("总计 ： " + 0);
        } else {
            mSumPrice.setText("总计 ： " + mStrSumPrice);
        }
        mDatas.clear();
        mDatas.addAll(bean.getList());
        if (mCommonAdapter != null) {
            mCommonAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initListener() {

        mCommonAdapter = new CommonAdapter<ShopCarBean.ListBean>(this, R.layout.activity_submit_order_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, ShopCarBean.ListBean dataBean, int position) {
                TextView mName = holder.getView(R.id.tv_name);
                TextView mWeight = holder.getView(R.id.tv_weight);
                TextView mPrice = holder.getView(R.id.tv_price);

                mName.setText(dataBean.getName());
                mWeight.setText(dataBean.getNum() + dataBean.getMunit());
                mPrice.setText(dataBean.getPrice());
            }
        };

        mRecyclerView.setAdapter(mCommonAdapter);

    }


    @Override
    protected void onNewIntent(Intent intent) {
        this.mAddrBean = (AddressBean) intent.getSerializableExtra(Constans.EXTRA_SUBMIT_ORDER);
        this.mClientBeanId = intent.getStringExtra(Constans.EXTRA_SUBMIT_ORDER_CLIENT_ID);
        if (mClientBeanId != null && mAddrBean != null) {
            showView();
        }
        super.onNewIntent(intent);
    }

    private void submitShopCar() {
        if (mClientBeanId == null || mAddrBean == null) {
            Toast.makeText(this, "请选择收货人", Toast.LENGTH_SHORT).show();
            return;
        }

        postDataToService(mClientBeanId, mAddrBean.getId(), mStrSumPrice);
    }

    private void postDataToService(String clientId, String mAddrBeanId, final String mStrSumPrice) {

        HttpManager.instance()
                .submitShopCar(clientId, mAddrBeanId, mStrSumPrice)
                .subscribe(new ErrorHandleSubscriber<GuidBean>() {
                    @Override
                    public void onNext(@NonNull GuidBean bean) {
                        Intent intent = new Intent(SubmitOrderActivity.this, CashActivity.class);
                        intent.putExtra(Constans.EXTRA_CASH_ID,bean.getId() );
                        intent.putExtra(Constans.EXTRA_CASH_GUID,bean.getGuid() );
                        intent.putExtra(Constans.EXTRA_CASH_PRICE, mStrSumPrice);
                        startActivity(intent);
                        finish();
                    }
                });
    }

    @OnClick({R.id.rl_infomation, R.id.btn_submit})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.rl_infomation:
                Intent intent = new Intent(this, ListClientActivity2.class);
                startActivity(intent);
                break;
            case R.id.btn_submit:
//                生成订单
                submitShopCar();
                break;
            default:
                break;
        }
    }

}
